package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户地址簿")
@RestController
@Slf4j
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/list")
    @ApiOperation("查询当前登录用户的所有地址信息")
    public Result list(){
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list=addressBookService.list(addressBook);
        return Result.success(list);
    }

    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result listDefault(){
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setIsDefault(1);
        List<AddressBook> list = addressBookService.list(addressBook);
        return Result.success(list);
    }

    @ApiOperation("根据id查询地址")
    @GetMapping("/{id}")
    public Result getById(@PathVariable String id){
        AddressBook a = addressBookService.getById(Long.parseLong(id));
        return Result.success(a);
    }

    @ApiOperation("新增地址")
    @PostMapping
    public Result add(@RequestBody AddressBook addressBook){
        addressBookService.add(addressBook);
        return Result.success();
    }

    @ApiOperation("修改地址")
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook){
        addressBookService.update(addressBook);
        return Result.success();
    }

    @ApiOperation("设置当前用户默认地址")
    @PutMapping("/default")
    public Result listDefault(@RequestBody AddressBook addressBook){
        addressBookService.updateIsDefault(addressBook);
        return Result.success();
    }

    @ApiOperation("删除地址")
    @DeleteMapping
    public Result delete(@RequestBody AddressBook addressBook){
        addressBookService.delete(addressBook);
        return Result.success();
    }

}
